var app = new Vue({
  el: ".login",
  data() {
    return {
      inputtext: {
        email: "", //邮箱
        password: "", //一次密码
        rpassword: "" //二次密码
      },
      istrue: true,
      ischeck: false,
      maskfalse: false,
      theuser: "",
      information: "",
      showprotocol: false,
      refID: "",
      isfalse: false,
      loading: false,
      invite: ""
    };
  },
  created() {
    if (
      window.sessionStorage.getItem("refererId") &&
      window.sessionStorage.getItem("refererId") != ""
    ) {
      this.refID = window.sessionStorage.getItem("refererId");
    }
  //   if(this.refID==""){
  //     this.invite=""
  // }else{
  //     this.invite=this.refID
  // }
  },
  mounted() {
    this.loading = true;
  },
  methods: {
    sendcode() {
      //发送验证码
      let lang = this.getCookie("lang");
      let regValid = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
      if (this.inputtext.email == undefined || this.inputtext.email == "") {
        let timer = setInterval(() => {
          document.getElementsByClassName("errorMessage")[0].className =
            "errorMessage";
          clearInterval(timer);
          timer = null;
        }, 2000);
        document.getElementsByClassName("errorMessage")[0].className =
          "errorMessage fadeInUp";
        if (lang == "en_US") {
          this.theuser = "Incomplete information";
          this.information = "Please fill in the complete information.";
        } else if (lang == "zh_TW") {
          this.theuser = "邮箱不能为空";
          this.information = "邮箱不能为空";
        } else {
          this.theuser = "邮箱不能为空";
          this.information = "邮箱不能为空";
        }
        return;
      }
      if (!regValid.test(this.inputtext.email)) {
        let timer = setInterval(() => {
          document.getElementsByClassName("errorMessage")[0].className =
            "errorMessage";
          clearInterval(timer);
          timer = null;
        }, 2000);
        document.getElementsByClassName("errorMessage")[0].className =
          "errorMessage fadeInUp";
        if (lang == "en_US") {
          this.theuser = "Incorrect email";
          this.information = "Please enter the correct email address.";
        } else if (lang == "zh_TW") {
          this.theuser = "郵箱不正確";
          this.information = "請您輸入正確的郵箱地址";
        } else {
          this.theuser = "邮箱不正确";
          this.information = "请您输入正确的邮箱地址";
        }
        return;
      }
      let params = {
        type: 6,
        sendTo: this.inputtext.email
      };
      axios.post("/comm/mail/send", params).then(res => {});
      this.isfalse = true;
      let time = 60;
      let that = this;
      document.getElementsByClassName("emailsendNumber")[0].style.background =
        "#e9e9e9";
      let sendtime = setInterval(function() {
        time -= 1;
        document.getElementsByClassName("emailsendNumber")[0].innerHTML =
          time + "s";
        if (time == 0) {
          if (lang == "en_US") {
            document.getElementsByClassName("emailsendNumber")[0].innerHTML =
              "send code";
          } else if (lang == "zh_TW") {
            document.getElementsByClassName("emailsendNumber")[0].innerHTML =
              "發送驗證碼";
          } else {
            document.getElementsByClassName("emailsendNumber")[0].innerHTML =
              "发送验证码";
          }
          document.getElementsByClassName(
            "emailsendNumber"
          )[0].style.background =
            "white";
          that.isfalse = false;
          clearInterval(sendtime);
        }
      }, 1000);
    },
    quickLogin: function(e) {
      //回车及Enter登录
      if (e.keyCode == 13 || e.keyCode == 108) {
        this.login();
      }
    },
    getCookie(cookiename) {
      //获取cookie
      var arr,
        reg = new RegExp("(^| )" + cookiename + "=([^;]*)(;|$)");
      if ((arr = document.cookie.match(reg))) {
        return unescape(arr[2]);
      } else {
        return null;
      }
    },
    CheckAgreement() {
      this.ischeck = !this.ischeck;
    },
    closeinfo() {
      let timer = setInterval(() => {
        document.getElementsByClassName("errorMessage")[0].className =
          "errorMessage";
        clearInterval(timer);
        timer = null;
      }, 2000);
      document.getElementsByClassName("errorMessage")[0].className =
        "errorMessage fadeInUp";
      this.theuser = "";
      this.information = "";
      let BODY = document.getElementsByTagName("body")[0];
      BODY.style.overflowY = "";
    },
    submit() {
      //请求注册
      var reg = /^\+?[1-9]\d*$/;
      let lang = this.getCookie("lang");
      let regValid = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
      if (!this.ischeck) {
        let timer = setInterval(() => {
          document.getElementsByClassName("errorMessage")[0].className =
            "errorMessage";
          clearInterval(timer);
          timer = null;
        }, 2000);
        document.getElementsByClassName("errorMessage")[0].className =
          "errorMessage fadeInUp";
        if (lang == "en_US") {
          this.theuser = "Registration Failed";
          this.information = "Please check the agreement.";
        } else if (lang == "zh_TW") {
          this.theuser = "註冊失敗";
          this.information = "請您勾選協議";
        } else {
          this.theuser = "注册失败";
          this.information = "请您勾选协议";
        }
        return;
      }
      if (
        this.inputtext.email == undefined ||
        this.inputtext.password == undefined ||
        this.inputtext.rpassword == undefined
      ) {
        let timer = setInterval(() => {
          document.getElementsByClassName("errorMessage")[0].className =
            "errorMessage";
          clearInterval(timer);
          timer = null;
        }, 2000);
        document.getElementsByClassName("errorMessage")[0].className =
          "errorMessage fadeInUp";
        if (lang == "en_US") {
          this.theuser = "Registration Failed";
          this.information = "Please fill in the complete information.";
        } else if (lang == "zh_TW") {
          this.theuser = "註冊失敗";
          this.information = "信息不完整,請您填好完整信息";
        } else {
          this.theuser = "注册失败";
          this.information = "信息不完整,请您填好完整信息";
        }
        return;
      }
      if (!regValid.test(this.inputtext.email)) {
        let timer = setInterval(() => {
          document.getElementsByClassName("errorMessage")[0].className =
            "errorMessage";
          clearInterval(timer);
          timer = null;
        }, 2000);
        document.getElementsByClassName("errorMessage")[0].className =
          "errorMessage fadeInUp";
        if (lang == "en_US") {
          this.theuser = "Registration Failed";
          this.information = "Please enter the correct email address.";
        } else if (lang == "zh_TW") {
          this.theuser = "註冊失敗";
          this.information = "郵箱不正確,請您輸入正確的郵箱地址";
        } else {
          this.theuser = "注册失败";
          this.information = "邮箱不正确,请您输入正确的邮箱地址";
        }
        return;
      }
      if (this.inputtext.password != this.inputtext.rpassword) {
        let timer = setInterval(() => {
          document.getElementsByClassName("errorMessage")[0].className =
            "errorMessage";
          clearInterval(timer);
          timer = null;
        }, 2000);
        document.getElementsByClassName("errorMessage")[0].className =
          "errorMessage fadeInUp";
        if (lang == "en_US") {
          this.theuser = "Registration Failed";
          this.information = "The two passwords are inconsistent.";
        } else if (lang == "zh_TW") {
          this.theuser = "註冊失敗";
          this.information = "兩次密碼輸入不壹致";
        } else {
          this.theuser = "注册失败";
          this.information = "两次密码输入不一致";
        }
        return;
      }
      if (
        this.inputtext.password.length < 6 ||
        this.inputtext.rpassword.length < 6
      ) {
        let timer = setInterval(() => {
          document.getElementsByClassName("errorMessage")[0].className =
            "errorMessage";
          clearInterval(timer);
          timer = null;
        }, 2000);
        document.getElementsByClassName("errorMessage")[0].className =
          "errorMessage fadeInUp";
        if (lang == "en_US") {
          this.theuser = "Registration Failed";
          this.information = "Password length cannot be less than 6";
        } else if (lang == "zh_TW") {
          this.theuser = "註冊失敗";
          this.information = "密碼長度不能小於6";
        } else {
          this.theuser = "注册失败";
          this.information = "密码长度不能小于6";
        }
        return;
      }
      /*
            if (document.getElementsByClassName("sencode")[0].value == "") {
                let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.theuser="Registration Failed"
                    this.information="Please enter verification code"
                } else if (lang == "zh_TW") {
                    this.theuser="註冊失敗"
                    this.information="請輸入驗證碼"
                } else {
                    this.theuser="注册失败"
                    this.information="请输入验证码"
                }
                return
            }
            */
      if (this.istrue == true) {
        let params = {
          email: this.inputtext.email,
          password: this.inputtext.password
          // checkCode:parseFloat(document.getElementsByClassName("sencode")[0].value)
        };
        if (this.invite && this.invite != "") {
          if (!reg.test(this.invite)) {
            let timer = setInterval(() => {
              document.getElementsByClassName("errorMessage")[0].className =
                "errorMessage";
              clearInterval(timer);
              timer = null;
            }, 2000);
            document.getElementsByClassName("errorMessage")[0].className =
              "errorMessage fadeInUp";
            if (lang == "en_US") {
              this.theuser = "Incorrect input ID";
              this.information = "Please enter the correct ID.";
            } else if (lang == "zh_TW") {
              this.theuser = "輸入ID有誤";
              this.information = "請您輸入正確ID";
            } else {
              this.theuser = "输入ID有误";
              this.information = "请您输入正确ID";
            }
            return
          }
          params.refererId=parseFloat(this.invite)
        }else{
            if (this.refID) {
                params.refererId = parseFloat(this.refID);
              }
              let isref = location.href.indexOf("ref");
              if (isref != -1) {
                params.refererId = parseInt(
                  location.href.slice(location.href.lastIndexOf("=") + 1)
                );
              }
        }
        axios.post("/user/register", params).then(res => {
          if (res.data.code == -10002) {
            let timer = setInterval(() => {
              document.getElementsByClassName("errorMessage")[0].className =
                "errorMessage";
              clearInterval(timer);
              timer = null;
            }, 2000);
            document.getElementsByClassName("errorMessage")[0].className =
              "errorMessage fadeInUp";
            if (lang == "en_US") {
              this.theuser = "Registration Failed";
              this.information = "Email already exists, please re-enter";
            } else if (lang == "zh_TW") {
              this.theuser = "註冊失敗";
              this.information = "郵箱已存在,請重新輸入";
            } else {
              this.theuser = "注册失败";
              this.information = "邮箱已存在,请重新输入";
            }
          } else if (res.data.code == 200) {
            location.href = "/m/singin";
            sessionStorage.removeItem("refererId");
            // request.getSession().removeAttribute("user");
            // request.getSession().invalidate();
          } else if (res.data.code == -10003) {
            let timer = setInterval(() => {
              document.getElementsByClassName("errorMessage")[0].className =
                "errorMessage";
              clearInterval(timer);
              timer = null;
            }, 2000);
            document.getElementsByClassName("errorMessage")[0].className =
              "errorMessage fadeInUp";
            if (lang == "en_US") {
              this.theuser = "Registration Failed";
              this.information = "Registration is wrong, please re-register.";
            } else if (lang == "zh_TW") {
              this.theuser = "註冊失敗";
              this.information = "註冊有誤，請重新註冊";
            } else {
              this.theuser = "注册失败";
              this.information = "注册有误，请重新注册";
            }
          }
        });
      } else {
        return;
      }
    }
  }
});
